/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AppointmentPanel.java
 *
 * Created on Aug 12, 2011, 8:40:00 PM
 */
package panels;

import conn.EMIS4Connection;
import exec.NotFoundException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import util.LoggedInUser;
import obj.LoginHistory;
import dao.LoginHistoryDao;
import obj.UpdateHistory;
import dao.UpdateHistoryDao;
import java.util.List;
import table.model.HistoryTableModel;
import table.model.UpdateTableModel;
import util.DashboardUtil;
import util.DateUtils;
import util.PersonRequested;
import dao.PersonDao;
import obj.Person;

/**
 *
 * @author John
 */
public class UserHistoryPanel extends javax.swing.JPanel {

    private static UserHistoryPanel instance;
    private LoginHistoryDao loginHistoryDao;
    private LoginHistory loginHistory;
    private List<LoginHistory> allHistory;
    private UpdateHistoryDao updateHistoryDao;
    private UpdateHistory updateHistory;
    private List<UpdateHistory> allUpdate;
    private HistoryTableModel loginHistoryTableModel;
    private UpdateTableModel updateHistoryTableModel;   //change tables model
    static int historyrows;
    DateUtils date;
    LoggedInUser loggedInUser = LoggedInUser.getInstance();
    EMIS4Connection conn = EMIS4Connection.getInstance();
    private PersonRequested pr = PersonRequested.getInstance();
    private int userID;
    private Person person;
    private PersonDao personDAO;

    /** Creates new form AppointmentPanel */
    public UserHistoryPanel() {
        super();
        initComponents();
        defineComponents();
        fillTableWithData();
    }

    private void defineComponents() {
        //m_tblAllAppointment    = new AppointmentTable();
        //m_tblTodaysAppointment = new AppointmentTable();

        loginHistoryTableModel = new HistoryTableModel();
        updateHistoryTableModel = new UpdateTableModel(); //change table model

        jTable1.setModel(loginHistoryTableModel);
        jTable2.setModel(updateHistoryTableModel);

    }

    private void fillTableWithData() {
        if (pr.getPerson() == null) {
            DashboardUtil.searchForPatient();
        }

        person = pr.getPerson();
        userID = person.getPersonID();

        conn.connect();

//Login History
        try {
            loginHistoryDao = new LoginHistoryDao();
            loginHistory = new LoginHistory();
            loginHistory.setPerson_ID(pr.getPerson().getPersonID());
            allHistory = loginHistoryDao.loadAll(conn.getConnection(), userID);

            for (LoginHistory item : allHistory) {
                // fill table
             //  loginHistory = allHistory.get(item);
               loginHistoryTableModel.addLoginHistory(item);
            }


        } catch (NotFoundException ex) {
            Logger.getLogger(UserHistoryPanel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        
      //Update history  
        
        try {
            updateHistoryDao = new UpdateHistoryDao();
            updateHistory = new UpdateHistory();
            updateHistory.setTableID(pr.getPerson().getPersonID());
            allUpdate = updateHistoryDao.loadAll(conn.getConnection(), userID);

            for (UpdateHistory item1 : allUpdate) {
                // fill table
             //  loginHistory = allHistory.get(item);
               updateHistoryTableModel.addUpdateHistory(item1);
            }


        } catch (NotFoundException ex) {
            Logger.getLogger(UserHistoryPanel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        


    }
        
       

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();

        jTabbedPane1.setName("jTabbedPane1"); // NOI18N

        jPanel1.setName("jPanel1"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Person ID", "IP Address", "Date", "Title 4"
            }
        ));
        jTable1.setName("jTable1"); // NOI18N
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 452, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 402, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Login History", jPanel1);

        jPanel2.setName("jPanel2"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null}
            },
            new String [] {
                "Table Name", "Table ID", "Field Name", "Action", "Old Value", "New Value", "Timestamp"
            }
        ));
        jTable2.setName("jTable2"); // NOI18N
        jScrollPane2.setViewportView(jTable2);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 452, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 414, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jTabbedPane1.addTab("Update History", jPanel2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 462, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(175, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 436, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    // End of variables declaration//GEN-END:variables
}
